package com.toy.abash.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.toy.abash.domain.Abash;
import com.toy.abash.domain.Reply;
import com.toy.abash.service.AbashManager;
import com.toy.abash.service.ReplyManager;
import com.toy.base.domain.User;
import com.toy.base.service.UserManager;
import com.toy.core.client.SystemFacade;
import com.toy.core.service.support.GridFSDBFileWrapper;
import com.toy.core.util.StringPool;
import com.toy.core.util.StringUtils;
import com.toy.core.web.BaseController;

@Controller
@RequestMapping("/abash")
public class PostController extends BaseController{
	
	public static String  MODULE_PUBLISH_PREFIX = "/abash/publish";
	
	@Autowired
	private AbashManager abashManager;
	
	@Autowired
	private ReplyManager replyManager;
	
	@Autowired
	private UserManager userManager;
	
	//编辑糗事
	@RequestMapping("/post/new")
	public String editPost(Model model){
		String username = SystemFacade.getSecurityService().getCurrentUsername();
		User user = userManager.getUserByUsername(username);
		model.addAttribute("user", user);
		return MODULE_PUBLISH_PREFIX + "/posts_edit";
	}
	
	//保存糗事
	@RequestMapping("/post/save")
	public String savePost(Abash entity, @RequestParam String tag,
			HttpServletRequest request) throws IOException{
		if (StringUtils.isNotBlank(tag)){
			entity.setTags(tag.split(StringPool.BLANK));
		}
		entity.setIpaddress(request.getRemoteAddr());
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		Map<String, MultipartFile> fileMap =  multipartRequest.getFileMap();
		Collection<MultipartFile> multipartFiles =  fileMap.values();
		abashManager.saveAbash(entity, new ArrayList<MultipartFile>(multipartFiles));
		return "redirect:/abash/dayhot/1";
	}
	
	//TIPS:显示图片
	//显示图片
	@RequestMapping("/images/{objectId}")
	public ResponseEntity<byte[]> responseImage(@PathVariable String objectId) throws Exception{
		GridFSDBFileWrapper gridFSDBFileWrapper =  abashManager.getGridFSDBFileByObjectId(objectId);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.set("Content-Disposition", "attachment; filename=\""
				+ new String(gridFSDBFileWrapper.getFilename().getBytes(), "ISO-8859-1")+ "\"");
		headers.set("Accept-ranges", "bytes");
		return new ResponseEntity<byte[]>(gridFSDBFileWrapper.getBytes(),headers,HttpStatus.OK);
	}
	
	//查看糗事
	@RequestMapping("/post/view/{postId}") 
	public String viewPost(@PathVariable String postId,Model model){
		Abash abash = abashManager.getById(Long.valueOf(postId));
		model.addAttribute("abash", abash);
		List<Reply> replies = replyManager.findReplyByPostId(Long.valueOf(postId));
		model.addAttribute("replies", replies);
		Map<Long,User> users = replyManager.findReplysUsers(replies);
		model.addAttribute("users", users);
		String username = SystemFacade.getSecurityService().getCurrentUsername();
		User user = userManager.getUserByUsername(username);
		model.addAttribute("user", user);
		return MODULE_PUBLISH_PREFIX + "/posts_view";
	}
}
